package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.PickCateType;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentIntellectualPropertyBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.MemberIntellectualPropertyBean;
import com.sgb.member.ui.activity.ClientPropertyActivity;
import com.sgb.member.ui.adapter.IntellectualPropertyAdapter;
import com.sgb.member.view_model.IntellectualPropertyModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.areapopup.GoodsSourceLocationEntity;
import com.swgk.core.view.areapopup.ThreeLevelAreaWindow;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.CopyUtils;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.view.selection.XChooseDialog;
import com.swgk.core.view.selection.XChooseEntity;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by：冯齐 on 2021/1/26 18:02
 * Description：知识产权
 */
public class IntellectualPropertyFragment extends AppLazyFragment implements MemberStatusView.OnChooseListener {

    @Inject
    IntellectualPropertyModel mModel;
    private FragmentIntellectualPropertyBinding mBinding;

    private int mCurIndex;

    //商标\专利\版权类型弹窗
    private String mNo1;
    //经营类型弹窗
    private String mNo3;
    // 地区
    private String mNo4;
    private String mNo5;
    // 领域分类
    private String mNo6;
    // 专利状态
    private String mNo7;

    private CommonPopEntity mAll;

    private IntellectualPropertyAdapter mIntellectualPropertyAdapter;

    // 经营地址Popup
    private ThreeLevelAreaWindow mAddressPop;
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData;

    public static IntellectualPropertyFragment newInstance(int index) {
        IntellectualPropertyFragment fragment = new IntellectualPropertyFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", index);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_intellectual_property, container, false);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        assert getArguments() != null;
        mCurIndex = getArguments().getInt("INDEX");
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("不限");
        switch (mCurIndex) {
            case 0: // 商标
                mBinding.popup1.setKeyName("商标类型");
                break;
            case 1: // 版权
                mBinding.popup1.setKeyName("版权类型");
                break;
            case 2: // 专利
                mBinding.popup1.setKeyName("专利类型");
                mBinding.popupField.setVisibility(View.VISIBLE);
                mBinding.popupStatus.setVisibility(View.VISIBLE);
                mBinding.popupAddress.setVisibility(View.GONE);
                break;
        }
        mBinding.popup1.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        // 经营类型
        mBinding.popup2.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        // 领域分类
        mBinding.popupField.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        // 专利状态
        mBinding.popupStatus.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        // 地区
        mBinding.popupAddress.initPopup(mBinding.grayLayout, mBinding.popLine, this);

        mBinding.popupAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (scrData == null) {
                    mModel.getAddressModel(true);
                } else {
                    showAddressPopup();
                }
            }
        });
        initAddressPopup();
        initRecycler();
    }

    private void initRecycler() {
        TextView textView = mBinding.emptyView.findViewById(R.id.no_data_count);
        textView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setPullRefreshEnabled(true);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mIntellectualPropertyAdapter = new IntellectualPropertyAdapter(getContext(), null);
        mBinding.recyclerView.setAdapter(mIntellectualPropertyAdapter);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
        mIntellectualPropertyAdapter.setOnItemClick(new AdapterOnItemClick<MemberIntellectualPropertyBean>() {
            @Override
            public void onItemClick(MemberIntellectualPropertyBean memberIntellectualPropertyBean, int position) {
                // 点击整个知识产权模块，跳转到对应的商标详情/专利详情/版权详情
                mModel.getUrlSetting("assetsDetail", memberIntellectualPropertyBean.getId());
            }
        });
    }

    //知识产权列表数据
    public void loadData(boolean isLoadMore) {
        String searchText = getActivity() == null ? "" : ((ClientPropertyActivity) getActivity()).getSearchText();
        switch (mCurIndex) {
            case 0: // 商标
                mModel.getMemberIntellectualPropertyRight(isLoadMore, searchText, mNo1, mNo3, mNo4, mNo5, mNo6, mNo7, "商标");
                break;
            case 1: // 版权
                mModel.getMemberIntellectualPropertyRight(isLoadMore, searchText, mNo1, mNo3, mNo4, mNo5, mNo6, mNo7, "版权");
                break;
            case 2: // 专利
                mModel.getMemberIntellectualPropertyRight(isLoadMore, searchText, mNo1, mNo3, mNo4, mNo5, mNo6, mNo7, "专利");
                break;
        }
    }

    @Override
    protected void onLazyLoad() {
        loadData(false);
    }

    //网络请求空数据处理
    public void showEmpty() {
        mIntellectualPropertyAdapter.clearDatas();
        mBinding.emptyView.setVisibility(View.VISIBLE);
    }

    public void setMemberIntellectualPropertyRight(List<MemberIntellectualPropertyBean> list, boolean isLoadMore, boolean isLastPage) {
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        if (isLoadMore) {
            mBinding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                mIntellectualPropertyAdapter.addDatas(list);
            }
            mBinding.recyclerView.setNoMore(isLastPage);
        } else {
            mBinding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mIntellectualPropertyAdapter.setDatas(list);
            } else {
                mIntellectualPropertyAdapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onLoadChooseData(String mKeyName) {
        switch (mKeyName) {
            case "商标类型":
            case "版权类型":
            case "专利类型":
                //商标\版权\专利类型接口请求
                mModel.getPropertyType(mCurIndex, true);
                break;
            case "经营类型":
                mModel.getServiceTypeList(true);
                break;
            case "领域分类":
                mModel.getFieldClassifyType(true);
                break;
            case "专利状态":
                mModel.getPatentStatus(true);
                break;
        }
    }

    @Override
    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
        int position = (int) objects[0];
        String no = position == 0 ? "" : entity.getNo();
        String id = position == 0 ? "" : entity.getId();
        String code = position == 0 ? "" : entity.getCode();
        switch (mKeyName) {
            case "商标类型":
            case "版权类型":
            case "专利类型":
                //商标\版权\专利类型接口请求
                if (!TextUtils.equals(no, mNo1)) {
                    mNo1 = no;
                    loadData(false);
                }
                break;
            case "经营类型":
                if (!TextUtils.equals(code, mNo3)) {
                    mNo3 = code;
                    loadData(false);
                }
                break;
            case "领域分类":
                if (!TextUtils.equals(id, mNo6)) {
                    mNo6 = id;
                    loadData(false);
                }
                break;
            case "专利状态":
                if (!TextUtils.equals(id, mNo7)) {
                    mNo7 = id;
                    loadData(false);
                }
                break;
        }
    }

    private void initAddressPopup() {
        mAddressPop = new ThreeLevelAreaWindow(getActivity());
        mAddressPop.setBgView(mBinding.grayLayout);
        mAddressPop.setMemberHeight();
        mAddressPop.setCitySingle();
        mAddressPop.setProvinceSingle();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnlyShowProAndCity(true);
        mAddressPop.setAllSingleSelect(true);
        mAddressPop.setThreeListIsGone(true);
        mAddressPop.setOnResultClick(new ThreeLevelAreaWindow.OnResultClickProxy() {
            @Override
            protected void result(GoodsSourceLocationEntity entity) {
                mNo4 = "";
                mNo5 = "";
                mBinding.popupAddress.updateChooseTextStatus(0, mBinding.popupAddress.getKeyName());
                if (!TextUtils.isEmpty(entity.getProvinceNo())) {
                    mNo4 = entity.getProvinceNo();
                    mBinding.popupAddress.updateChooseTextStatus(1, entity.getProvinceName());
                    if (!TextUtils.isEmpty(entity.getCityNo())) {
                        mNo5 = entity.getCityNo();
                        mBinding.popupAddress.updateChooseTextStatus(1, entity.getCityName());
                    }
                }
                //刷新数据
                loadData(false);
            }
        });
        mAddressPop.setOnDismissListener(() -> {
            mBinding.popLine.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(mNo4) || !TextUtils.isEmpty(mNo5)) {
                mBinding.popupAddress.updateChooseTextStatus(1, null);
            } else {
                mBinding.popupAddress.updateChooseTextStatus(0, mBinding.popupAddress.getKeyName());
            }
        });
    }

    private void showAddressPopup() {
        mBinding.popLine.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(mNo4) || !TextUtils.isEmpty(mNo5)) {
            mBinding.popupAddress.updateChooseTextStatus(-2, null);
        } else {
            mBinding.popupAddress.updateChooseTextStatus(-1, mBinding.popupAddress.getKeyName());
        }
        mAddressPop.showPopwindow0(mBinding.popLine);
    }

    //商标\版权\专利类型接口回调
    public void setPropertyTypeList(boolean isShowPopup, List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        mBinding.popup1.setData(data);
        if (isShowPopup) {
            mBinding.popup1.showPop();
        }
    }

    // 经营类型接口回调
    public void setServiceTypeList(boolean isShowPopup, List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        data.add(0, mAll);
        mBinding.popup2.setData(data);
        if (isShowPopup) {
            mBinding.popup2.showPop();
        }
    }

    // 专利- 领域分类接口回调
    public void setFieldClassifyType(boolean isShowPopup, List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        data.add(0, mAll);
        mBinding.popupField.setData(data);
        if (isShowPopup) {
            mBinding.popupField.showPop();
        }
    }

    // 获取专利-专利状态接口回调
    public void setPatentStatus(boolean isShowPopup, List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        mBinding.popupStatus.setData(data);
        if (isShowPopup) {
            mBinding.popupStatus.showPop();
        }
    }

    public void setAddressData(boolean isShowPopup, List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data, List<CommonPopEntity> entities) {
        if (data != null && data.size() > 0) {
            scrData = data;
            entities.add(0, mAll);
            mAddressPop.setData(data, true);
            if (isShowPopup) {
                showAddressPopup();
            }
        }
    }
}
